package org.davidliebman.android.awesomecube;

import java.util.ArrayList;

import android.content.Context;
import android.os.Handler;
import android.util.Log;

import javax.microedition.khronos.opengles.GL10;

public class Pad {
	
	//ArrayList<Mesh> mList = new ArrayList<Mesh>();
	public static int ARRAY_SIZE = 12;
	public SimplePad [] mMeshArray = new SimplePad[ARRAY_SIZE] ;
	
	public Context mContext;
	GL10 gl;
	//Picking mPicker;
	public boolean mUsePicking = false;
	public boolean mLastUsePicking = false;
	public boolean mShouldFlash = false;
	public PointXYZ mVec = new PointXYZ(0,0,0);
	public PadQueue mQueue = new PadQueue();
	public int mChosenColor = 0;
	public int mChosenMove = 0;
	public int mChosenArrow = -1;
	public Handler mHandler;
	
	public Pad(Context c, GL10 gl) {
		this.gl = gl;
		mContext = c;
		// note: left and right are reversed!!
		mMeshArray [0] =  new SimplePad(c, OrientationArray.MOVE_F_CCW ,TextureLibrary.DOWN, TextureLibrary.SELECTED_DOWN, false, Mesh.FACE_PLANE_X, -1,-2, 1);
		mMeshArray [1] = new SimplePad(c, OrientationArray.MOVE_B_CW , TextureLibrary.DOWN, TextureLibrary.SELECTED_DOWN ,false, Mesh.FACE_PLANE_X, -1, -2, -1);
		mMeshArray [2] = new SimplePad(c, OrientationArray.MOVE_U_CW , TextureLibrary.RIGHT, TextureLibrary.SELECTED_RIGHT ,false, Mesh.FACE_PLANE_X, -1, 1, -2);
		mMeshArray [3] = new SimplePad(c, OrientationArray.MOVE_D_CCW, TextureLibrary.RIGHT, TextureLibrary.SELECTED_RIGHT, false, Mesh.FACE_PLANE_X, -1, -1, -2);//
		mMeshArray [4] = new SimplePad(c, OrientationArray.MOVE_B_CCW, TextureLibrary.UP, TextureLibrary.SELECTED_UP ,false, Mesh.FACE_PLANE_Y,  2, 2, -1);//
		mMeshArray [5] = new SimplePad(c, OrientationArray.MOVE_F_CW,  TextureLibrary.UP, TextureLibrary.SELECTED_UP, false, Mesh.FACE_PLANE_Y, 2, 2, 1);//
		mMeshArray [6] = new SimplePad(c, OrientationArray.MOVE_R_CW,  TextureLibrary.RIGHT, TextureLibrary.SELECTED_RIGHT,  false, Mesh.FACE_PLANE_Y,  1, 2, -2);//
		mMeshArray [7] = new SimplePad(c, OrientationArray.MOVE_L_CCW, TextureLibrary.RIGHT, TextureLibrary.SELECTED_RIGHT, false, Mesh.FACE_PLANE_Y, -1, 2, -2);
		mMeshArray [8] = new SimplePad(c, OrientationArray.MOVE_U_CCW, TextureLibrary.LEFT, TextureLibrary.SELECTED_LEFT, false, Mesh.FACE_PLANE_Z,  2, 1, 2);//
		mMeshArray [9] = new SimplePad(c, OrientationArray.MOVE_D_CW,  TextureLibrary.LEFT, TextureLibrary.SELECTED_LEFT,false, Mesh.FACE_PLANE_Z, 2, -1, 2);//
		mMeshArray [10] = new SimplePad(c, OrientationArray.MOVE_R_CCW, TextureLibrary.DOWN, TextureLibrary.SELECTED_DOWN,false, Mesh.FACE_PLANE_Z,  1, -2, 2);//
		mMeshArray [11] = new SimplePad(c, OrientationArray.MOVE_L_CW,  TextureLibrary.DOWN, TextureLibrary.SELECTED_DOWN, false, Mesh.FACE_PLANE_Z, -1, -2, 2);//
		for(int i = 0; i < ARRAY_SIZE; i ++ ) mMeshArray[i].preBuildMesh();

	}
	
	public int size() {
		return ARRAY_SIZE;
	}
	
	public Mesh get(int i) {
		return mMeshArray[i];
	}
	
	public void usePicking(boolean mPicking) {
		boolean mOldPicking = mUsePicking;
		mUsePicking = mPicking;
		if(mOldPicking != mUsePicking) {
			//switch Mesh for extremely small buttons
			switchMesh() ;
		}
	}
	
	public void render(GL10 gl, int i, PointXYZ mVec, int mTexture) {
		this.mVec = mVec;
		render(gl,i);
	}
	
	public void render(GL10 gl, int i, PointXYZ mVec) {
		this.mVec = mVec;
		render(gl,i);
	}
	
	
	public void render(GL10 gl, int i) {
		this.gl = gl;
		SimplePad mPad = (SimplePad)mMeshArray[i];
		
		gl.glPushMatrix();
		mPad.scaleAndRotateP0(gl);
		
		// blink chosen arrow...
		if (i == mChosenArrow || mMeshArray[i].mMoveEnumeration == mChosenMove) {
			if ( mShouldFlash ) {
				mPad.setSelectedArrow();
			}
			else {
				mPad.setBlankArrow();
			}
		}
		else {
			mPad.setStandardArrow();
		}
		
		// picking rendering in red...
		if(this.mUsePicking ){
			if (i == 0) mPad.disableForColors(gl);
			mPad.makeColors(gl, i);
		 
			mPad.buildMesh(gl);

			if (i >= ARRAY_SIZE - 1 ) mPad.enableAfterColors(gl);
		
			//add mVec to mQueue...
			PointXYZ mPoint = mQueue.tail();
			if ((int)mPoint.x != (int)mVec.x || (int)mPoint.y != (int)mVec.y) {
				mQueue.add(mVec);
			}
			if (mQueue.size() > 0) {
				PointXYZ mP = mQueue.poll();
				int mTemp = Picking.readColor(gl, (int)mP.x,(int)mP.y);
				if (mTemp != 0 ) {
					mChosenColor = mTemp;
					findEnumeration();
				}
				
			}
			
			
		}
		else {
			mPad.buildMesh(gl);
		}
		gl.glPopMatrix();
	}
	
	public void switchMesh() {
		// effect 11,0, maybe others
		
		if (mUsePicking ) {

			mMeshArray [0] =  new SimplePad(mContext, OrientationArray.MOVE_F_CCW ,TextureLibrary.DOWN, TextureLibrary.SELECTED_DOWN, false, Mesh.FACE_PLANE_X, -1,-3, 1, MeshPart.SHAPE_OBLONG_A);
			mMeshArray [1] = new SimplePad(mContext, OrientationArray.MOVE_B_CW , TextureLibrary.DOWN, TextureLibrary.SELECTED_DOWN ,false, Mesh.FACE_PLANE_X, -1, -3, -1, MeshPart.SHAPE_OBLONG_A);
			mMeshArray [2] = new SimplePad(mContext, OrientationArray.MOVE_U_CW , TextureLibrary.RIGHT, TextureLibrary.SELECTED_RIGHT ,false, Mesh.FACE_PLANE_X, -1, 1, -3, MeshPart.SHAPE_OBLONG_B);
			mMeshArray [3] = new SimplePad(mContext, OrientationArray.MOVE_D_CCW, TextureLibrary.RIGHT, TextureLibrary.SELECTED_RIGHT, false, Mesh.FACE_PLANE_X, -1, -1, -3, MeshPart.SHAPE_OBLONG_B);//
			mMeshArray [4] = new SimplePad(mContext, OrientationArray.MOVE_B_CCW, TextureLibrary.UP, TextureLibrary.SELECTED_UP ,false, Mesh.FACE_PLANE_Y,  3, 2, -1, MeshPart.SHAPE_OBLONG_A);//
			mMeshArray [5] = new SimplePad(mContext, OrientationArray.MOVE_F_CW,  TextureLibrary.UP, TextureLibrary.SELECTED_UP, false, Mesh.FACE_PLANE_Y, 3, 2, 1, MeshPart.SHAPE_OBLONG_A);//
			mMeshArray [6] = new SimplePad(mContext, OrientationArray.MOVE_R_CW,  TextureLibrary.RIGHT, TextureLibrary.SELECTED_RIGHT,  false, Mesh.FACE_PLANE_Y,  1, 2, -3, MeshPart.SHAPE_OBLONG_B);//
			mMeshArray [7] = new SimplePad(mContext, OrientationArray.MOVE_L_CCW, TextureLibrary.RIGHT, TextureLibrary.SELECTED_RIGHT, false, Mesh.FACE_PLANE_Y, -1, 2, -3, MeshPart.SHAPE_OBLONG_B);
			mMeshArray [8] = new SimplePad(mContext, OrientationArray.MOVE_U_CCW, TextureLibrary.LEFT, TextureLibrary.SELECTED_LEFT, false, Mesh.FACE_PLANE_Z,  3, 1, 2, MeshPart.SHAPE_OBLONG_A);//
			mMeshArray [9] = new SimplePad(mContext, OrientationArray.MOVE_D_CW,  TextureLibrary.LEFT, TextureLibrary.SELECTED_LEFT,false, Mesh.FACE_PLANE_Z, 3, -1, 2, MeshPart.SHAPE_OBLONG_A);//
			mMeshArray [10] = new SimplePad(mContext, OrientationArray.MOVE_R_CCW, TextureLibrary.DOWN, TextureLibrary.SELECTED_DOWN,false, Mesh.FACE_PLANE_Z,  1, -3, 2, MeshPart.SHAPE_OBLONG_B);//
			mMeshArray [11] = new SimplePad(mContext, OrientationArray.MOVE_L_CW,  TextureLibrary.DOWN, TextureLibrary.SELECTED_DOWN, false, Mesh.FACE_PLANE_Z, -1, -3, 2, MeshPart.SHAPE_OBLONG_B);//
			for(int i = 0; i < ARRAY_SIZE; i ++ ) mMeshArray[i].preBuildMesh();
			
//			mMeshArray [0] =  new SimplePad(mContext, OrientationArray.MOVE_F_CCW ,TextureLibrary.DOWN,TextureLibrary.SELECTED_DOWN, false, Mesh.FACE_PLANE_X, -1,-2, 1, MeshPart.SHAPE_OBLONG_A);
//			mMeshArray [11] = new SimplePad(mContext, OrientationArray.MOVE_L_CW,  TextureLibrary.DOWN, TextureLibrary.SELECTED_DOWN,false, Mesh.FACE_PLANE_Z, -1, -2, 2, MeshPart.SHAPE_OBLONG_B);//
//			mMeshArray [0].preBuildMesh();
//			mMeshArray [11].preBuildMesh();
		}
		else if (!mUsePicking ) {
			mMeshArray [0] =  new SimplePad(mContext, OrientationArray.MOVE_F_CCW ,TextureLibrary.DOWN, TextureLibrary.SELECTED_DOWN, false, Mesh.FACE_PLANE_X, -1,-2, 1);
			mMeshArray [1] = new SimplePad(mContext, OrientationArray.MOVE_B_CW , TextureLibrary.DOWN, TextureLibrary.SELECTED_DOWN ,false, Mesh.FACE_PLANE_X, -1, -2, -1);
			mMeshArray [2] = new SimplePad(mContext, OrientationArray.MOVE_U_CW , TextureLibrary.RIGHT, TextureLibrary.SELECTED_RIGHT ,false, Mesh.FACE_PLANE_X, -1, 1, -2);
			mMeshArray [3] = new SimplePad(mContext, OrientationArray.MOVE_D_CCW, TextureLibrary.RIGHT, TextureLibrary.SELECTED_RIGHT, false, Mesh.FACE_PLANE_X, -1, -1, -2);//
			mMeshArray [4] = new SimplePad(mContext, OrientationArray.MOVE_B_CCW, TextureLibrary.UP, TextureLibrary.SELECTED_UP ,false, Mesh.FACE_PLANE_Y,  2, 2, -1);//
			mMeshArray [5] = new SimplePad(mContext, OrientationArray.MOVE_F_CW,  TextureLibrary.UP, TextureLibrary.SELECTED_UP, false, Mesh.FACE_PLANE_Y, 2, 2, 1);//
			mMeshArray [6] = new SimplePad(mContext, OrientationArray.MOVE_R_CW,  TextureLibrary.RIGHT, TextureLibrary.SELECTED_RIGHT,  false, Mesh.FACE_PLANE_Y,  1, 2, -2);//
			mMeshArray [7] = new SimplePad(mContext, OrientationArray.MOVE_L_CCW, TextureLibrary.RIGHT, TextureLibrary.SELECTED_RIGHT, false, Mesh.FACE_PLANE_Y, -1, 2, -2);
			mMeshArray [8] = new SimplePad(mContext, OrientationArray.MOVE_U_CCW, TextureLibrary.LEFT, TextureLibrary.SELECTED_LEFT, false, Mesh.FACE_PLANE_Z,  2, 1, 2);//
			mMeshArray [9] = new SimplePad(mContext, OrientationArray.MOVE_D_CW,  TextureLibrary.LEFT, TextureLibrary.SELECTED_LEFT,false, Mesh.FACE_PLANE_Z, 2, -1, 2);//
			mMeshArray [10] = new SimplePad(mContext, OrientationArray.MOVE_R_CCW, TextureLibrary.DOWN, TextureLibrary.SELECTED_DOWN,false, Mesh.FACE_PLANE_Z,  1, -2, 2);//
			mMeshArray [11] = new SimplePad(mContext, OrientationArray.MOVE_L_CW,  TextureLibrary.DOWN, TextureLibrary.SELECTED_DOWN, false, Mesh.FACE_PLANE_Z, -1, -2, 2);//
			for(int i = 0; i < ARRAY_SIZE; i ++ ) mMeshArray[i].preBuildMesh();
			
		}
	}
	
	public void findEnumeration() {
		mChosenMove = 0;
		for (int i = 0; i < ARRAY_SIZE; i ++ ) {
			if (mChosenColor > Picking.thresholdLower(i) && mChosenColor < Picking.thresholdUpper(i)) {
				mChosenMove = ((SimplePad)mMeshArray[i]).getMoveEnumeration();
				mChosenArrow = i;
			}
		}
		mChosenColor = 0;
		if (mChosenMove != 0) {
			mHandler.sendEmptyMessage(mChosenMove);
		}
		
	}
	
	public void setShouldFlash(boolean mFlash) {
		mShouldFlash = mFlash;
	}
}

